// ignore_for_file: import_of_legacy_library_into_null_safe

import 'package:flutter/material.dart';
// ignore: unused_import
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:flutter_widget/custom/custom_button.dart';
import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';
// ignore: unnecessary_import, unused_import
import 'package:universal_html/html.dart' as html;

// ignore: must_be_immutable
class PdffilePage extends StatefulWidget {
  String name;
  PdffilePage({Key? key, required this.name}) : super(key: key);

  @override
  // ignore: no_logic_in_create_state
  _PdfPageState createState() => _PdfPageState();
}

class _PdfPageState extends State<PdffilePage> {
  final pdf = pw.Document();

 
  writeOnPdf() {
    pdf.addPage(
      pw.MultiPage(
        pageFormat: PdfPageFormat.a4,
        margin: const pw.EdgeInsets.all(32),
        build: (pw.Context context) {
          return <pw.Widget>[
            pw.Header(
              level: 0,
              child: pw.Row(
                mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                children: <pw.Widget>[
                  pw.Text('Geeksforgeeks', textScaleFactor: 2),
                ],
              ),
            ),
            pw.Header(level: 1, text: 'What is Lorem Ipsum?'),
            pw.Paragraph(text: ' 1'),
            pw.Paragraph(text: '2'),
            pw.Header(level: 1, text: 'This is Header'),
            pw.Paragraph(text: '3'),
            pw.Paragraph(text: '4'),
            pw.Padding(
              padding: const pw.EdgeInsets.all(10),
            ),
            pw.Table.fromTextArray(
              context: context,
              data: const <List<String>>[
                <String>['Year', 'Sample'],
                <String>['SN0', 'GFG1'],
                <String>['SN1', 'GFG2'],
                <String>['SN2', 'GFG3'],
                <String>['SN3', 'GFG4'],
              ],
            ),
          ];
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("PDF"),
      ),
      body: Container(
        padding: const EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            CustomButton(
              content: const Text(
                'Preview PDF',
              ),
              onPressed: () async {
                writeOnPdf();
                await savePdf();
                Directory documentDirectory =
                    await getApplicationDocumentsDirectory();
                String documentPath = documentDirectory.path;
                String fullPath = "$documentPath/example.pdf";
                // ignore: avoid_print
                print(fullPath);
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => PdfPreviewScreen(
                      path: fullPath,
                    ),
                  ),
                );
              },
            ),
            SizedBox(height: 20,),
            CustomButton(
              content: const Text(
                'Open PDF',
              ),
              onPressed: () async {
                final blob = html.Blob([pdf.save()], 'application/pdf');
                final url = html.Url.createObjectUrlFromBlob(blob);
                html.window.open(url, '_blank');
                html.Url.revokeObjectUrl(url);
              },
            ),
             SizedBox(height: 20,),
            CustomButton(
              content: const Text(
                'Download PDF',
              ),
              onPressed: () async {
                final blob = html.Blob([pdf.save()], 'application/pdf');
                final url = html.Url.createObjectUrlFromBlob(blob);
                final anchor =
                    html.document.createElement('a') as html.AnchorElement
                      ..href = url
                      ..style.display = 'none'
                      ..download = 'some_name.pdf';
                html.document.body!.children.add(anchor);
                anchor.click();
                html.document.body!.children.remove(anchor);
                html.Url.revokeObjectUrl(url);
              },
            ),
          ],
        ),
      ),
    );
  }

  Future<void> savePdf() async {
    Directory documentDirectory = await getApplicationDocumentsDirectory();
    String documentPath = documentDirectory.path;
    File file = File("$documentPath/example.pdf");
    // file.writeAsBytesSync(
    //   pdf.save(),
    // );
  }
}








class PdfPreviewScreen extends StatelessWidget {
  final String path;
// ignore: use_key_in_widget_constructors
  const PdfPreviewScreen({required this.path});
  @override
  Widget build(BuildContext context) {
    return PDFViewerScaffold(
      path: path,
    );
  }
}

